<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Http\Model\User;
use Illuminate\Http\Request;

use App\Http\Requests;
use Illuminate\Support\Facades\Input;

class IndexController extends Controller
{
    public function index()
    {
        return view('admin.index');
    }
    public function info()
    {
        return view('admin.info');
    }
    public function pass(Request $request)
    {
        $message = [
            'user_pass.required' => '旧密码不能为空',
            'new_pass.required'=> '新密码不能为空',
            'new_pass.between'=> '新密码必须在6到20位之间',
            'new_pass.confirmed' => '两次密码输入不一致',
        ];
        if($input = $request->all()){
            $this->validate($request, [
                'user_pass' => 'required',
                'new_pass' => 'required|between:6,20|confirmed',
            ], $message);
            $user = User::first();
            $pass = $user->user_pass;
            if(md5($input['user_pass']) == $pass){
                $new_pass = md5($input['new_pass']);
                $user->user_pass = $new_pass;
                $user->save();
                return back()->with('errors', '密码修改成功');
            }else{
                return back()->with('errors', '原密码错误');
            }
        }else{
            return view('admin.pass');
        }
    }
}
